package com.homework.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/17 23:28
 */
@Configuration
@Import(ResourceConfig.class)
@ComponentScan(basePackages = "com.homework")
@PropertySource(value = "classpath:db.properties")
@Slf4j
public class SpringConfig {
    @Value("com.mysql.cj.jdbc.Driver")
    private String dbDriver;
    @Value("jdbc:mysql://localhost:3306/chenyuhui")
    private String dbUrl;
    @Value("root")
    private String dbUser;
    @Value("123456")
    private String dbPassword;

    @Bean
    @Scope("prototype")
    public Connection getConnection(){
        try {
            Class.forName(this.dbDriver);
            return DriverManager.getConnection(this.dbUrl,this.dbUser,this.dbPassword);
        } catch (Throwable e){
            log.error(e.getMessage());
            return null;
        }
    }

}
